package com.rightsidetech.system.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rightsidetech.commom.base.ResponseData;
import com.rightsidetech.commom.enums.CodeIdEnum;
import com.rightsidetech.commom.utils.DateUtil;
import com.rightsidetech.system.dtos.request.BatteryCheckAddReq;
import com.rightsidetech.system.dtos.request.BatteryCheckHandleReq;
import com.rightsidetech.system.dtos.request.BatteryCheckReq;
import com.rightsidetech.system.entity.SysAgentInfo;
import com.rightsidetech.system.entity.SysBatteryCheck;
import com.rightsidetech.system.entity.SysCompanyArea;
import com.rightsidetech.system.entity.store.SysStoreInfo;
import com.rightsidetech.system.mapper.SysAgentInfoMapper;
import com.rightsidetech.system.mapper.SysBatteryCheckMapper;
import com.rightsidetech.system.mapper.SysCompanyAreaMapper;
import com.rightsidetech.system.mapper.SysStoreInfoMapper;
import com.rightsidetech.system.service.SysBatteryCheckService;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Package: com.rightsidetech.system.service.impl
 * @ClassName: SysBatteryCheckServiceImpl
 * @Author: yinj
 * @CreateTime: 2022/01/05 16:15
 */
@Service("sysBatteryCheckServiceImpl")
public class SysBatteryCheckServiceImpl implements SysBatteryCheckService {

    @Resource
    private SysBatteryCheckMapper sysBatteryCheckMapper;
    @Resource
    private SysStoreInfoMapper sysStoreInfoMapper;
    @Resource
    private SysCompanyAreaMapper sysCompanyAreaMapper;
    @Resource
    private SysAgentInfoMapper sysAgentInfoMapper;

    @Override
    public ResponseData<PageInfo<SysBatteryCheck>> page(BatteryCheckReq req) {
        PageHelper.startPage(req.getPageNo(),req.getPageSize());
        List<SysBatteryCheck> list = sysBatteryCheckMapper.selectAll(req);
        return ResponseData.success(new PageInfo<>(list));
    }

    @Override
    public List<SysBatteryCheck> selectList(BatteryCheckReq request){
        return sysBatteryCheckMapper.selectAll(request);
    }

    @Override
    public List<SysBatteryCheck> exBatteryCheck(BatteryCheckReq req) {
        return sysBatteryCheckMapper.selectAll(req);
    }

    @Override
    public ResponseData handle(BatteryCheckHandleReq request) {
        SysBatteryCheck sysBatteryCheck = sysBatteryCheckMapper.selectByPrimaryKey(request.getId());
        if (ObjectUtils.isEmpty(sysBatteryCheck)){
            return ResponseData.fail(CodeIdEnum.EMPTY_DATA);
        }
        if (!sysBatteryCheck.getAgentId().equals(request.getAgentId())){
            return ResponseData.fail(CodeIdEnum.EMPTY_DATA);
        }
        if (sysBatteryCheck.getStatus() == 1){
            return ResponseData.fail("已处理无需重复处理");
        }
        SysBatteryCheck batteryCheck = new SysBatteryCheck();
        batteryCheck.setId(request.getId());
        batteryCheck.setWarranty(request.getWarranty());
        batteryCheck.setDischargeTime(request.getDischargeTime());
        batteryCheck.setCheckResult(request.getCheckResult());
        batteryCheck.setReportPicture(request.getReportPicture());
        batteryCheck.setCheckTime(DateUtil.parseDateStr(request.getCheckTime()));
        batteryCheck.setStatus((byte) 1);
        batteryCheck.setHandleAt(new Date());
        batteryCheck.setHandleName(request.getHandleName());
        sysBatteryCheckMapper.updateByPrimaryKeySelective(batteryCheck);
        return ResponseData.success();
    }

    @Override
    public ResponseData add(BatteryCheckAddReq request) {
        SysStoreInfo sysStoreInfo = sysStoreInfoMapper.selectByPrimaryKey(request.getStoreId());
        if (ObjectUtils.isEmpty(sysStoreInfo)){
            return ResponseData.fail(CodeIdEnum.NonStoreInfo);
        }
        SysBatteryCheck batteryCheck = new SysBatteryCheck();
        batteryCheck.setAreaId(sysStoreInfo.getAreaId());
        SysCompanyArea sysCompanyArea = sysCompanyAreaMapper.selectByPrimaryKey(sysStoreInfo.getId());
        batteryCheck.setAreaName(ObjectUtils.isEmpty(sysCompanyArea) ? " " : sysCompanyArea.getName());
        batteryCheck.setAgentId(sysStoreInfo.getSysAgentInfoId());
        SysAgentInfo sysAgentInfo = sysAgentInfoMapper.selectByPrimaryKey(sysStoreInfo.getSysAgentInfoId());
        batteryCheck.setAgentName(ObjectUtils.isEmpty(sysAgentInfo) ? " " : sysAgentInfo.getName());
        batteryCheck.setStoreId(request.getStoreId());
        batteryCheck.setStoreName(sysStoreInfo.getStoreName());
        batteryCheck.setBatteryName(request.getBatteryName());
        batteryCheck.setBatteryCode(request.getBatteryCode());
        batteryCheck.setSpecs(request.getSpecs());
        batteryCheck.setCreatedAt(new Date());
        sysBatteryCheckMapper.insertSelective(batteryCheck);
        return ResponseData.success();
    }

    @Override
    public SysBatteryCheck selectByPrimaryKey(Long id) {
        return sysBatteryCheckMapper.selectByPrimaryKey(id);
    }
}
